package gogogo.liwushuo.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import gogogo.liwushuo.R;
import gogogo.liwushuo.adapter.VpAdapter;
import gogogo.liwushuo.bean.ScollerBean;
import gogogo.liwushuo.constant.Resource;
import gogogo.liwushuo.util.OKHttpUtils;

public class HomeFragment extends Fragment {


    public static HomeFragment newInstance() {
        
        Bundle args = new Bundle();
        
        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }
    
    @InjectView(R.id.toolbar_home)
    Toolbar toolbarHome;
    @InjectView(R.id.scollerview_iv)
    ImageView scollerviewIv;
    @InjectView(R.id.viewpager)
    ViewPager viewpager;
    @InjectView(R.id.tblayout)
    TabLayout tblayout;

    private static final String TAG = "mtag";
    private OKHttpUtils okHttpUtils;
    private Context mContext;
    private String jsonDataScollerView;
    private ScollerBean scollerBean;
    private List<ScollerBean.DataBean.ChannelsBean> channels;
    private List<SubHomeFragment> list;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.inject(this, view);
        return view;
    }


    private void initData() {
        mContext = getActivity();
//        Log.i(TAG, "initData: " + (mContext == null));
        Log.i(TAG, "initData: ");
        okHttpUtils = OKHttpUtils.newInstance(mContext);
        okHttpUtils.getAsyncMainData(Resource.URL_HORIZTSCOLLVIEW, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {
//                Log.i(TAG, "onFailure: ");
            }

            @Override
            public void onResponse(String result) {
                jsonDataScollerView = result;
                Log.i(TAG, "onResponse: " + result);
                scollerBean = new Gson().fromJson(jsonDataScollerView, ScollerBean.class);
                channels = scollerBean.getData().getChannels();
                initFragmentList();
            }
        });
    }

    private void initFragmentList() {
        list = new ArrayList<>();
        for (int i = 0; i < channels.size(); i++) {
            list.add(SubHomeFragment.newInstance(i));
        }
        viewpager.setAdapter(new VpAdapter(getChildFragmentManager(),list));
        tblayout.setupWithViewPager(viewpager);
//        viewpager.setOffscreenPageLimit(1);
        Log.i(TAG, "initFragmentList: " +list.size());
        for (int i = 0; i < list.size(); i++) {
            TabLayout.Tab tab = tblayout.getTabAt(i);
            tab.setText(channels.get(i).getName());
        }

    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        toolbarHome.setTitle("");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
